package com.dts.evaluate.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * TimeUtils
 * 
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-8-24
 */
public class TimeUtils {

    public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat MY_DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static final SimpleDateFormat DATE_FORMAT_DATE    = new SimpleDateFormat("yyyy-MM-dd");

    private TimeUtils() {
        throw new AssertionError();
    }

    /**
     * 比较两个字符串对应的时间 哪个大
     * @param timeStr1
     * @param timeStr2
     * @return
     */
    public static int compare(String timeStr1,String timeStr2){
        try {
            Date date1 = MY_DEFAULT_DATE_FORMAT.parse(timeStr1);
            Date date2 = MY_DEFAULT_DATE_FORMAT.parse(timeStr2);

            return date1.compareTo(date2);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0;

    }

    /**
     * 判断字符串对应的时间  是否小于当前时间
     * @param timeStr
     * @return
     */
    public static boolean isLessThanCurrentTime(String timeStr) {
        try {
            Date date = MY_DEFAULT_DATE_FORMAT.parse(timeStr);
            return date.getTime() - System.currentTimeMillis() < 0 ? true : false ;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * long time to string
     * 
     * @param timeInMillis
     * @param dateFormat
     * @return
     */
    public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
        return dateFormat.format(new Date(timeInMillis));
    }

    /**
     * long time to string, format is {@link #DEFAULT_DATE_FORMAT}
     * 
     * @param timeInMillis
     * @return
     */
    public static String getTime(long timeInMillis) {
        return getTime(timeInMillis, DEFAULT_DATE_FORMAT);
    }

    /**
     * get current time in milliseconds
     * 
     * @return
     */
    public static long getCurrentTimeInLong() {
        return System.currentTimeMillis();
    }

    /**
     * get current time in milliseconds, format is {@link #DEFAULT_DATE_FORMAT}
     * 
     * @return
     */
    public static String getCurrentTimeInString() {
        return getTime(getCurrentTimeInLong());
    }

    /**
     * get current time in milliseconds
     * 
     * @return
     */
    public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
        return getTime(getCurrentTimeInLong(), dateFormat);
    }
}
